/*
 * ===================================================================
 *  
 *      Filename:  util.h
 *
 *   Description:  This file is used to work as an superman  :)
 *                 Most of the internal interface is defined in this 
 *                 file,while the public interface is defined in the 
 *                 log_client_api.h
 *
 *       Version:  1.0
 *        Create:  2010/07/13
 *
 *        Author:  Keven Zhu     kevenzhu@tencent.com
 *     Copyright:  Reserved by Tencent.com Co.ltd
 *          
 * ====================================================================
 */
#ifndef __LOG_CLIENT_UTIL__
#define __LOG_CLIENT_UTIL__

#define OK     1
#define FALSE  0

#ifndef bool
#define bool int
#endif

#define  POLICY_FILE_PATH  "policy.conf"

#define HPLOG_SRV_PORT       10514
#define HPLOG_CLT_PORT       41501
#define HPLOG_SRN_MNG        41502
#define HPLOG_CLT_MULC       41503

#define POLICY_MULT_GROUP    "224.0.0.218"

#define HPLOG_SEM_KEY       ((0x4321)|(0x56<<16)|(0x27<<24))
#define HPLOG_SHMM_KEY      ((0x1234)|(0x65<<16)|(0x72<<24))
#define HPLOG_MSG_QUEUE     ((0x2341)|(0x56<<16)|(0x87<<24))
#define INV_HANDLER         -1
#define MAX_LOG_MSG_COUNTER 1024
#define MAX_LOG_MSG_SIZE    1024
/*max length for the actual data*/
#define MAX_LOG_DATA_SIZE   768

#define DAEMON_PROCESS      0
#define CLIENT_PROCESS      1

#define SEARCH_ALL          0
#define SEARCH_EXT          1

#define COLOR_DISABLED      3
#define COLOR_ENABLED       1

/*control the IPC method,they can NOT be enabled at the same time*/
#define IPC_MSG 1
#define IPC_SHM 0

/*for the log doctor*/
#define DEBUG_NONE          0x00
#define DEBUG_PRINT         0x01
#define DEBUG_LOCAL         0x02
extern  int doctor_level;

/*share memory map*/
/*|----------------------------------------------------------------------------------------
 *|      management header     |     msg        |        msg        |           msg       |
 *|----------------------------------------------------------------------------------------
 *|         msg        |            msg        |         msg       |         ...          |
 *|---------------------------------------------------------------------------------------*/
typedef struct share_mem_ctl_header_
{
    unsigned long max_queue;
    unsigned long max_queue_mask;
    unsigned long enqueue_counter;
    unsigned long dequeue_counter;
    /*the msg list point is kept in the process local header!!! see below*/
    //char**        log_list;
}share_mem_ctl_header;

/*we must keep an copy of the header in local process,this is the point*/
typedef struct local_share_mem_ctl_header_
{
    unsigned long* max_queue;
    unsigned long* max_queue_mask;
    unsigned long* enqueue_counter;
    unsigned long* dequeue_counter;
    char*          log_list[MAX_LOG_MSG_COUNTER];
}local_share_mem_ctl_header;


/* Load the log client daemon
 *  
 * return value:
 * positive means failure,error could be found in the head file or with the help of log_error();
 * 0 means ok(launched or exist)*/
extern int load_hplog_client_deamon();
extern int finitialize_hplog_mutex_sem();

/*semphore for the process sync*/
/* post the semphore(unlock)
 *  
 * return value:
 * positive means failure,error could be found in the head file or with the help of plogerr();
 * 0 means ok(launched)*/
extern int hplog_sem_wirter_post();
extern int hplog_sem_reader_post();

/* fetch the semphore(lock)
 *
 * return value:
 * positive means failure,error could be found in the head file or with the help of plogerr();
 * 0 means ok(launched)*/
extern int hplog_sem_writer_wait();
extern int hplog_sem_reader_wait();

/*policy main routine*/
extern pthread_t init_log_policy(int policy_fd);
extern int finit_log_policy();
extern int set_daemon_log_level(int log_level);

/*load balance modules*/
extern int init_loadbalance();
extern int update_destination_addr(int list_type);
extern int finit_loadbalance();
extern unsigned int get_destination();

/*backup modules*/
extern int init_log_backup();
extern int finit_log_backup();
extern int get_backup_status();
extern int send_log_to_backup(char* buff);
extern int flush_backup_to_srv(int sockfd);

/*tcp modules*/
extern int send_log_via_tcp(char* msg);
extern int init_protocol_tcp();
extern int finit_protocol_tcp();
extern int flush_backup_to_srv_with_tcp();
extern int reinit_protocol_tcp(unsigned int srv_addr);

/*udp modules*/
extern int send_log_via_udp(char* msg);
extern int init_protocol_udp();
extern int finit_protocol_udp();
extern int reinit_protocol_udp(unsigned int srv_addr);

/*color modules*/
extern int init_log_color();
extern int finit_log_color();
extern int get_color_status();
extern int set_color_status(int status,unsigned char* reg);
extern int color_msg(char* msg);

/*debug doctor modules*/
extern int init_log_doctor();
extern int finit_log_doctor();
extern int log_doctor(int level,const char* fmt,...);

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*send packet to the client using private protocol*/
/*|-------------------------------------------------------------------
 *|protocol(4bits)|version(4bits)|operation(byte)|file version(string)|
 *|-------------------------------------------------------------------|
 *|protocol(4bits)|version(4bits)|operation(byte)|sequence(string)    |
 *|-------------------------------------------------------------------|
 *|length(4 bytes)|                      data                         |
 *|-------------------------------------------------------------------|
 *protocol
 0x01   color
 0x02   policy
 0x03   log_level
 0x07   destination
 *version
 0x1
 *operation
 for color:                        for policy:            for log_level:                   for destination:
 0x01 enable  request              0x01 update request    value(0-7)  set the log_level    0x01  update the destination
 0x02 enable  reply                0x02 update accept
 0x03 disable request              0x03 update reject
 0x04 disable reply                0x04 update data
 0x05 request reject               0x05 update finish
*/
/*add some special description for the color*/
/* -----------------------------------------------
 * protocol|version|enable request|regular express|
 * -----------------------------------------------*/
#define CS_PROTOCOL_POLICY       0x02
#define CS_PROTOCOL_COLOR        0x01
#define CS_PROTOCOL_LOG          0x03
#define CS_PROTOCOL_LB           0x07
#define CS_VERSION     0x01
#define REQUEST_COLOR  0x01
#define ENABLE_COLOR_REQUEST     0x01
#define ENABLE_COLOR_REPLY       0x02
#define DISABLE_COLOR_REQUEST    0x03
#define DISABLE_COLOR_REPLY      0x04
#define REJECT_COLOR_REQUEST     0x05

#define REQUEST_POLICY 0x02
#define UPDATE_POLICY_REQUEST 0x01
#define UPDATE_POLICY_ACCEPT  0x02
#define UPDATE_POLICY_REJECT  0x03
#define UPDATE_POLICY_DATA    0x04
#define UPDATE_POLICY_FINISH  0x05

#define UPDATE_DESTINATION_REQUESTION  0x01


#endif

